/**
 * TMS 物流管理系统 - API调用封装
 * Transportation Management System
 * 
 * 功能：调用后端API接口
 */

// ========================================
// 物流跟踪相关API
// ========================================
class TMSAPI {
    // 获取跟踪信息
    static async getTrackingInfo(trackingNumber) {
        try {
            const response = await fetch(`/api/method/upsystem.upsystem.api.get_tracking_info?tracking_number=${trackingNumber}`);
            return await response.json();
        } catch (error) {
            console.error('Failed to get tracking info:', error);
            throw error;
        }
    }
    
    // 获取跟踪历史
    static async getTrackingHistory(trackingNumber) {
        try {
            const response = await fetch(`/api/method/upsystem.upsystem.api.get_tracking_history?tracking_number=${trackingNumber}`);
            return await response.json();
        } catch (error) {
            console.error('Failed to get tracking history:', error);
            throw error;
        }
    }
    
    // 获取承运商信息
    static async getCarrierInfo(trackingNumber) {
        try {
            const response = await fetch(`/api/method/upsystem.upsystem.api.get_carrier_info?tracking_number=${trackingNumber}`);
            return await response.json();
        } catch (error) {
            console.error('Failed to get carrier info:', error);
            throw error;
        }
    }
    
    // 批量跟踪查询
    static async batchTracking(trackingNumbers) {
        try {
            const response = await fetch('/api/method/upsystem.upsystem.api.batch_tracking', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ tracking_numbers: trackingNumbers })
            });
            return await response.json();
        } catch (error) {
            console.error('Failed to batch tracking:', error);
            throw error;
        }
    }
    
    // 获取国家代码列表
    static async getCountryCodes() {
        try {
            const response = await fetch('/api/method/upsystem.upsystem.api.get_country_codes');
            return await response.json();
        } catch (error) {
            console.error('Failed to get country codes:', error);
            throw error;
        }
    }
    
    // 获取物流产品列表
    static async getLogisticsProducts(countryCode = null) {
        try {
            const url = countryCode 
                ? `/api/method/upsystem.upsystem.api.get_logistics_products?country_code=${countryCode}`
                : '/api/method/upsystem.upsystem.api.get_logistics_products';
            const response = await fetch(url);
            return await response.json();
        } catch (error) {
            console.error('Failed to get logistics products:', error);
            throw error;
        }
    }
    
    // 计算物流费用
    static async calculateLogisticsFee(queryData) {
        try {
            const response = await fetch('/api/method/upsystem.upsystem.api.calculate_logistics_fee', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(queryData)
            });
            return await response.json();
        } catch (error) {
            console.error('Failed to calculate logistics fee:', error);
            throw error;
        }
    }
    
    // 创建订单
    static async createOrder(orderData) {
        try {
            const response = await fetch('/api/method/upsystem.upsystem.api.create_order', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(orderData)
            });
            return await response.json();
        } catch (error) {
            console.error('Failed to create order:', error);
            throw error;
        }
    }
    
    // 测试云途API连接
    static async testYTConnection() {
        try {
            const response = await fetch('/api/method/upsystem.upsystem.api.test_yt_connection');
            return await response.json();
        } catch (error) {
            console.error('Failed to test YT connection:', error);
            throw error;
        }
    }
}

// ========================================
// 导出到全局
// ========================================
window.TMS = {
    API: TMSAPI
};